package com.syh.jn.scm.service.impl.downstream.base;

import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.validator.ValidationUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.downstream.entity.base.DownBusinessEntityExtGoodsInfo;
import com.syh.jn.scm.domain.downstream.repository.base.DownBusinessEntityExtGoodsInfoRepository;
import com.syh.jn.scm.dto.downstream.base.DownBusinessEntityExtGoodsInfoMto;
import com.syh.jn.scm.dto.downstream.base.DownBusinessEntityExtGoodsInfoSto;
import com.syh.jn.scm.dto.downstream.base.DownBusinessEntityExtGoodsInfoVo;
import com.syh.jn.scm.service.api.downstream.base.DownBusinessEntityExtGoodsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品扩展编码
 */
@Service
@Transactional
public class DownBusinessEntityExtGoodsInfoServiceImpl extends AbstractServiceImpl<DownBusinessEntityExtGoodsInfoSto, DownBusinessEntityExtGoodsInfoMto, DownBusinessEntityExtGoodsInfoVo, DownBusinessEntityExtGoodsInfo, Long> implements DownBusinessEntityExtGoodsInfoService {
    @Autowired
    private DownBusinessEntityExtGoodsInfoRepository downBusinessEntityExtGoodsInfoRepository;

    @Override
    protected AggregateRepository<DownBusinessEntityExtGoodsInfo, Long> getAggregateRepository() {
        return downBusinessEntityExtGoodsInfoRepository;
    }

    /**
     * 保存商品扩展编码
     *
     * @param sto
     * @return
     */
    @Override
    public Long save(DownBusinessEntityExtGoodsInfoSto sto) {
        ExtAssert.notNull(sto, "对象为空");
        ValidationUtils.validate(sto);

        //根据id查找商品信息
        DownBusinessEntityExtGoodsInfo goodsExtCode = new DownBusinessEntityExtGoodsInfo(sto);
        return downBusinessEntityExtGoodsInfoRepository.add(goodsExtCode);
    }

    /**
     * 修改商品扩展编码
     *
     * @param mto
     */
    @Override
    public void update(DownBusinessEntityExtGoodsInfoMto mto) {
        ExtAssert.notNull(mto, "对象为空");
        ValidationUtils.validate(mto);

        //根据id查找商品信息
        DownBusinessEntityExtGoodsInfo goodsExtCode = downBusinessEntityExtGoodsInfoRepository.getById(mto.getId());
        ExtAssert.notNull(goodsExtCode, "对象为空");

        goodsExtCode.modify(mto);

        downBusinessEntityExtGoodsInfoRepository.update(goodsExtCode);
    }

    /**
     * 根据商品id查找扩展编码
     *
     * @param id
     * @return
     */
    @Override
    public DownBusinessEntityExtGoodsInfoVo getCodeByGoodsId(Long id,Long businessId) {
        return downBusinessEntityExtGoodsInfoRepository.getCodeByGoodsId(id,businessId);
    }
}
